package com.sugar.MultiThread.a10threadsafe2;


/**
 * 实现方式一：继承Thread
 *
 * 同步方法
 * @author felix
 * @date 2025/2/26 09:32
 */
public class MyRun implements Runnable {

    static int ticket = 100;

    @Override
    public void run() {
        while (true) {
            if (method()) break;
        }
    }

    private synchronized boolean method() {
        if(ticket == 0){
            return true;
        }else{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ticket--;
            System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票！！！！");
        }
        return false;
    }
}
